<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--仓库查询数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="库位编号" prop="locationCode">
            <el-input
              v-model="queryParams.locationCode"
              placeholder="请输入库位编号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="库位名称" prop="locationName">
            <el-input
              v-model="queryParams.locationName"
              placeholder="请输入库位名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="所属仓库" prop="warehouseId">
            <el-select
              v-model="queryParams.warehouseId"
              placeholder="所属仓库"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in warehouseList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['wms:location:add']"
            >新增</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="dataList" >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="库位编号" align="center" key="locationCode" prop="locationCode" />
          <el-table-column label="库位名称" align="center" key="locationName" prop="locationName"/>
          <el-table-column label="所属仓库" align="center" key="warehouseId">
            <template slot-scope="scope">
              <dict-tag :options="warehouseList" :value="scope.row.warehouseId"/>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" key="locationStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_warehouse_status" :value="scope.row.locationStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createDate" >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" key="remark" prop="remark"/>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['wms:location:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['wms:location:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="库位编号" prop="locationCode">
              <el-input v-model="form.locationCode" placeholder="请输入库位编号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库位名称" prop="locationName">
              <el-input v-model="form.locationName" placeholder="请输入库位名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属仓库">
              <el-select v-model="form.warehouseId" placeholder="请选择所属仓库" clearable>
                <el-option
                  v-for="dict in warehouseList"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-select v-model="form.locationStatus" placeholder="请选择库位状态" clearable>
                <el-option
                  v-for="dict in dict.type.wms_warehouse_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addObj, delObj, editObj, findAllList, findById } from "@/api/wms/location";
import { getDictInfo } from "@/api/wms/warehouse";

export default {
  name: "Location",
  dicts: ['wms_warehouse_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 数据列表
      dataList: [],
      // 仓库列表
      warehouseList: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        locationCode: undefined,
        locationName: undefined,
        warehouseId: undefined
      },
      // 表单校验
      rules: {
        locationCode: [
          { required: true, message: "库位编号不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '库位编号必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        locationName: [
          { required: true, message: "库位名称不能为空", trigger: "blur" }
        ],
        warehouseStatus: [
          { required: true, message: "库位状态不能为空", trigger: "blur" },
          { required: true, message: "库位状态不能为空", trigger: "change" }
        ],
        warehouseId: [
          { required: true, message: "所属仓库不能为空", trigger: "blur" },
          { required: true, message: "所属仓库不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
  },
  methods: {
    getWarehouseList(){
      getDictInfo().then(response => {
          this.warehouseList = response.data;
        }
      );
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      findAllList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        locationCode: undefined,
        locationName: undefined,
        warehouseId: undefined,
        locationStatus: '10',
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      findById(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位";
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            editObj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addObj(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除库位编号为"' + row.locationCode + '"的数据项？').then(function() {
        return delObj(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
